#include "GameEngine.h"

using namespace std;

namespace gaim{
       
    
    
    GameEngine::GameEngine(int fps, int x, int y, int w, int h){
            fps_ = fps;   
            SDL_Init( SDL_INIT_EVERYTHING ); 
            mainScreen_ = SDL_SetVideoMode(w, h, 0, SDL_SWSURFACE);                          
                                 
    }  

    

	GameEngine::~GameEngine(){
		for(std::set<Sprite*>::iterator i = sprites_.begin(); i != sprites_.end(); ++i)
			sprites_.erase(i);
		SDL_Quit();
	}
	
	

	void GameEngine::add(Sprite* s){
		sprites_.insert(s);
	}
	
	
	
	void GameEngine::remove(Sprite* s){
        //remove s from set using delete
        //if you remove it using erase, it'll call the destructor, which is good, but our vector is of sprite pointers, which dont have destructors 
        
	}

	void GameEngine::run(){

		bool quit = false;
		int tickInterval = 1000 / fps_;
		Uint32 nextTick, lastTick;
		
		//deltaTime
        double dt = 0.03;

		while (!quit) {
			nextTick = SDL_GetTicks() + tickInterval;
			
			dt = SDL_GetTicks() - lastTick;
            
            SDL_Event eve;
        	while(SDL_PollEvent(&eve)){
                switch (eve.type){
                    case SDL_KEYDOWN:
                        if (eve.key.keysym.sym == SDLK_ESCAPE){
				            quit = true;
				            break;
                     }
                     switch (eve.key.keysym.sym){//prepare for keypresses
                        case SDLK_RIGHT:
                        break;
                        case SDLK_LEFT:
                        break;
                        case SDLK_UP:
                        break;
                        case SDLK_DOWN:
                        break;
                    }
                    break;
 
                    case SDL_KEYUP:
                      switch (eve.key.keysym.sym){//prepare for key releases
                        case SDLK_RIGHT:
                        break;
                        case SDLK_LEFT:
                        break;
                        case SDLK_UP:
                        break;
                        case SDLK_DOWN:
                        break;
                      }
                        break;
			     	case SDL_QUIT:
				    	quit = true;
				    	break;
                }//switch

        	} // while pollevent
        	
        	//For each sprite that exists, every loop calls the update method
        	for(std::set<Sprite*>::iterator i = sprites_.begin(); i != sprites_.end(); ++i){
			    (*i)->update(sprites_, tickInterval/1000.0);  //The collection of sprites is sent along
            }
            
            lastTick = SDL_GetTicks();
            
			SDL_FillRect(mainScreen_, NULL, SDL_MapRGB(mainScreen_->format, 255,255,255));
			for(std::set<Sprite*>::const_iterator i = sprites_.begin(); i != sprites_.end(); ++i)
				(*i)->draw(mainScreen_);
			SDL_Flip(mainScreen_);

			int delay = nextTick - SDL_GetTicks();
			if (delay > 0)
				SDL_Delay(delay);
			
			cout << dt << endl;
			
		} // while

	} // run

}//GameEng
